﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace OpenWeb.Dal.Models
{
  public interface IEntity
  {
    /// <summary>
    /// The entity's unique (and URL-safe) public identifier
    /// </summary>
    /// <remarks>
    /// This is the identifier that should be exposed via the web, etc.
    /// </remarks>
    string Key { get; }
  }

  public abstract class EntityBase<TId> : IEntity, IEquatable<EntityBase<TId>>
  where TId : struct
  {

    [Key]
    public virtual TId Id
    {
      get
      {
        if (_id == null && typeof(TId) == typeof(Guid))
          _id = Guid.NewGuid();

        return _id == null ? default(TId) : (TId)_id;
      }
      protected set { _id = value; }
    }
    private object _id;

    [StringLength(50)]//Unique, 
    public virtual string Key
    {
      get { return _key = _key ?? GenerateKey(); }
      protected set { _key = value; }
    }
    private string _key;

    protected virtual string GenerateKey()
    {
      return Guid.NewGuid().ToString();// KeyGenerator.Generate();
    }

    bool IEquatable<EntityBase<TId>>.Equals(EntityBase<TId> other)
    {
      throw new NotImplementedException();
    }
  }
}
